package com.nai365.nart.product.service.impl;

import com.nai365.nart.product.dto.TransferResultDto;
import com.nai365.nart.product.entity.SkuInfo;
import com.nai365.nart.product.service.SkuInfoService;
import lombok.AllArgsConstructor;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@AllArgsConstructor
public class RabbitMQReciver {


	private final SkuInfoService skuInfoService;

	@RabbitListener(queues = {"chianQueue"})
	public void reviceSkuId(TransferResultDto transferResultDto) {
		System.out.println("--------------------------------------------------chianQueue "+transferResultDto);
		String[] split = transferResultDto.getRequestId().split("-");
		SkuInfo skuInfo = skuInfoService.getById(split[1]);

		if(split[0].equals("1")||split[0].equals("2")||split[0].equals("3")){
			skuInfo.setPurchaseTime(LocalDateTime.now());
		}
		//修改sku 表里面的ower adress

		skuInfo.setTxHash(transferResultDto.getTxHash());
		skuInfo.setOwnerAddress(transferResultDto.getTo());
		skuInfo.setOwnerCount(skuInfo.getOwnerCount()+1);
		skuInfo.setTxId(transferResultDto.getTxId());
		skuInfo.setOwnerId(Long.valueOf(split[2]));
		skuInfoService.saveOrUpdate(skuInfo);

//
//		LambdaUpdateWrapper<SkuInfo> skuInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//		skuInfoLambdaUpdateWrapper
//				.eq(SkuInfo::getSkuId,transferResultDto.getRequestId())
//				.set(SkuInfo::getTxHash,transferResultDto.getTxHash())
//				.set(SkuInfo::getOwnerAddress,transferResultDto.getTo())
//				.set(SkuInfo::getOwnerCount,)
//				.set(SkuInfo::getTxId,transferResultDto.getTxId());
//		skuInfoService.update(skuInfoLambdaUpdateWrapper);
	}
}
